﻿using Abp.Dependency;
using Batman.Cms.Core.Commands;
using Batman.Jianbei.Forum.Core.ChatRoom;
using Batman.Jianbei.Forum.Core.Datas;
using Batman.Jianbei.Forum.DTOS.Chatroom;
using Batman.Jianbei.Forum.Services.ChatRoom;
using Batman.Jianbei.Forum.SignalR;
using Dapper;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
using Quartz;
using System.Linq;
using System.Threading.Tasks;

namespace Batman.Jianbei.Forum.Jobs
{
    /// <summary>
    /// 树洞首页显示房间列表和人数 并通过singr发送到前端
    /// </summary>
    [DisallowConcurrentExecution]
    public class RoomCounterJob : ITransientDependency, IJob
    {
        public async Task Execute(IJobExecutionContext context)
        {
            var _hub = IocManager.Instance.Resolve<IHubContext<ChatHub>>();

            var sql = CommandPool.GetSQL("get_room_list_and_count");

            using (var ctx = new ChatRoomDbFactory().CreateDbContext(null))
            {
                using (var conn = ctx.Database.GetDbConnection())
                {
                    var result = conn.Query<RoomItemDTO>(sql).ToList();
                    await _hub.Clients.All.SendAsync("TREE_HOLE_INDEX_LIST", result);
                }
            }
        }
    }
}
